// 上面这个代码处理过度动画（默认加上不用管）
document.addEventListener('DOMContentLoaded', () => {
  setTimeout(() => {
    document.body.classList.add('sidenav-pinned')
    document.body.classList.add('ready')
  }, 200)
})

// 每个页面都会导入该js 统一的设置写这里即可
//基地址
axios.defaults.baseURL = 'http://ajax-api.itheima.net'
//封装轻提示
function tip(msg) {
  const toastDIV = document.querySelector('#myToast')
  toastDIV.children[0].innerHTML = msg
  const toast = new bootstrap.Toast(toastDIV, {
    delay: 1000
  })
  toast.show()
}
const spandom = document.querySelector('.navbar-nav span.font-weight-bold')
if (spandom) {
  spandom.innerHTML = localStorage.getItem('username')
}
const logout = document.querySelector('#logout')
if (logout) {
  logout.addEventListener('click', function () {
    localStorage.clear()
    location.href = './login.html'
  })
}
//请求拦截器
axios.interceptors.request.use(
  function (config) {
    //自动添加请求头
    const token = localStorage.getItem('token')
    if (token) {
      config.headers.Authorization = token
    }
    return config
  },
  function (error) {
    return Promise.reject(error)
  }
)
axios.interceptors.response.use(
  function (response) {
    return response.data
  },
  function (error) {
    // console.dir(error);
    if (error.response.status == '401') {
      location.href = '../login.html'
      localStorage.clear()
    } else {
      return Promise.reject(error)
    }
  }
)